package com.echoesnet.echoesshortvideo.views;

import android.app.Activity;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.echoesnet.echoesshortvideo.R;


/**
 * Created by yqh on 2017/3/20.
 */

public class InputTextMsgDialog extends DialogFragment implements View.OnClickListener
{
    private EditText inputText;
    private TextView say2user;
    private TextView dialogContent;
    private Button btnSend;
    private String inputHint = "";
    private Activity mActivity;

    private InputMethodManager imm;

    private IOnInputFinishedListener inputFinishedListener;

    @Override
    public void onStart()
    {
        super.onStart();
        Window win = getDialog().getWindow();
        // 一定要设置Background，如果不设置，window属性设置无效
        win.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);

        WindowManager.LayoutParams params = win.getAttributes();
        params.gravity = Gravity.BOTTOM;
        // 使用ViewGroup.LayoutParams，以便Dialog 宽度充满整个屏幕
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        win.setAttributes(params);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState)
    {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getDialog().getWindow().getAttributes().windowAnimations = R.style.dialogAnim;
        View view = inflater.inflate(R.layout.input_text_dialog, container, false);
        inputText = view.findViewById(R.id.inputText);
        btnSend = view.findViewById(R.id.btnSendMsg);
        say2user = view.findViewById(R.id.say2user);
        dialogContent = view.findViewById(R.id.dialogContent);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        afterViews();
    }

    private void afterViews()
    {
        mActivity = getActivity();
        inputText.requestFocus();
        imm = (InputMethodManager) mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        say2user.setOnClickListener(this);
        dialogContent.setOnClickListener(this);
        btnSend.setOnClickListener(this);
        if (!TextUtils.isEmpty(inputHint))
            inputText.setHint(inputHint);
        inputText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                if (s.length() > 0)
                {
                    btnSend.setBackgroundResource(R.drawable.round_corner10_c0312);
                }
                else
                {
                    btnSend.setBackgroundResource(R.drawable.round_corner10_c0331);
                }

            }

            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });


    }

    public void setInputHint(String hint)
    {
        this.inputHint = hint;
        if (inputText != null)
            inputText.setHint(hint);
    }

    @Override
    public void show(FragmentManager manager, String tag)
    {
        super.show(manager, tag);

        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                InputMethodManager inputManager = (InputMethodManager) inputText.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(inputText, 0);
                inputText.requestFocus();
            }
        }, 300);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.say2user:
                // TODO: 2018/4/24 跳转粉丝列表 选择@的人
                break;
            case R.id.dialogContent:
                InputMethodManager imm = (InputMethodManager) inputText.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(inputText.getWindowToken(), 0);
                dismiss();
                break;
            case R.id.btnSendMsg:
                if (inputFinishedListener != null)
                {
                    inputFinishedListener.onInputFinished(inputText.getText().toString().trim());
                }
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog)
    {
        if (imm != null)
            imm.hideSoftInputFromWindow(inputText.getWindowToken(), 0);
        super.onDismiss(dialog);

    }

    public void setOnInputFinishedListener(IOnInputFinishedListener listener)
    {
        inputFinishedListener = listener;
    }

    public interface IOnInputFinishedListener
    {
        void onInputFinished(String inputContent);
    }

}
